using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {
    public class DialogWindow : EditorWindow {

        public static DialogWindow ShowWindow(string title) {
            var window = GetWindow<DialogWindow>(title, true);
            window.titleContent = new(title);
            // window.ShowModalUtility(); // 使窗口变为模态，禁止操作其他窗口
            return window;
        }

        public static DialogWindow ShowWindow(string title, int width, int height) {
            var window = ShowWindow(title);
            window.titleContent = new(title);
            var p = window.position;
            window.position = new Rect(p.x, p.y, width, height);
            return window;
        }

        private ScrollView container;
        private Action<int> callback; // 关闭时触发的回调，点右上角X返回0，其它根据按钮编号传回1、2、3……
        private int closeFlag;

        public void UseTemplate_1Button(string buttonText_1, VisualElement body, Action<int> callback, bool useModal = true) {
            var bottomBar = new VisualElement();
            bottomBar.style.flexDirection = FlexDirection.Row;
            bottomBar.style.minHeight = 23;

            Button b1 = new Button(() => {
                closeFlag = 1;
                this.Close();
            });
            b1.text = buttonText_1;
            bottomBar.Add(b1);

            UseTemplate(body, bottomBar, callback, useModal);
        }

        public void UseTemplate_2Button(string buttonText_1, string buttonText_2, VisualElement body, Action<int> callback, bool useModal = true) {
            var bottomBar = new VisualElement();
            bottomBar.style.flexDirection = FlexDirection.Row;
            bottomBar.style.minHeight = 23;

            Button b1 = new Button(() => {
                closeFlag = 1;
                this.Close();
            });
            b1.text = buttonText_1;
            bottomBar.Add(b1);

            Button b2 = new Button(() => {
                closeFlag = 2;
                this.Close();
            });
            b2.text = buttonText_2;
            bottomBar.Add(b2);

            UseTemplate(body, bottomBar, callback, useModal);
        }

        public void UseTemplate_3Button(string buttonText_1, string buttonText_2, string buttonText_3, VisualElement body, Action<int> callback, bool useModal = true) {
            var bottomBar = new VisualElement();
            bottomBar.style.flexDirection = FlexDirection.Row;
            bottomBar.style.minHeight = 23;

            Button b1 = new Button(() => {
                closeFlag = 1;
                this.Close();
            });
            b1.text = buttonText_1;
            bottomBar.Add(b1);

            Button b2 = new Button(() => {
                closeFlag = 2;
                this.Close();
            });
            b2.text = buttonText_2;
            bottomBar.Add(b2);

            Button b3 = new Button(() => {
                closeFlag = 3;
                this.Close();
            });
            b3.text = buttonText_3;
            bottomBar.Add(b3);

            UseTemplate(body, bottomBar, callback, useModal);
        }

        private void UseTemplate(VisualElement body, VisualElement bottomBar, Action<int> callback, bool useModal) {
            rootVisualElement.Clear();
            this.callback = callback;

            container = new ScrollView();
            container.style.flexGrow = 1;

            rootVisualElement.Add(container);
            rootVisualElement.Add(bottomBar);

            container.Add(body);

            if (useModal) {
                ShowModalUtility(); // 使窗口变为模态，禁止操作其他窗口
            }
        }

        private void OnDisable() {
            callback?.Invoke(closeFlag);
        }
    }
}